package aleksandar.djuric.entities;

import java.awt.Color;

public class Attribute {
	private String description;
	private int min;
	private int max;
	private int meanValue;
	private double trueValue;
	private Color color;
	private String tabs;
	
	public Attribute(String s){
		description = s;
		trueValue = 0;
		if(s.equalsIgnoreCase("Disastrous"))
		{
			min = 1;
			max = 21;
			tabs = "\t\t";
		}
		else if(s.equalsIgnoreCase("Useless"))
		{
			min = 21;
			max = 31;
			tabs = "\t\t";
		}
		else if(s.equalsIgnoreCase("Very Poor"))
		{
			min = 31;
			max = 41;
			tabs = "\t\t";
		}
		else if(s.equalsIgnoreCase("Poor"))
		{
			min = 41;
			max = 51;
			tabs = "\t\t";
		}
		else if(s.equalsIgnoreCase("Mediocre"))
		{
			min = 51;
			max = 61;
			tabs = "\t\t";
		}
		else if(s.equalsIgnoreCase("Means Business"))
		{
			min = 61;
			max = 71;
			tabs = "\t";
		}
		else if(s.equalsIgnoreCase("Good"))
		{
			min = 71;
			max = 81;
			tabs = "\t\t";
		}
		else if(s.equalsIgnoreCase("Very Good"))
		{
			min = 81;
			max = 86;
			tabs = "\t\t";
		}
		else if(s.equalsIgnoreCase("Superb"))
		{
			min = 86;
			max = 91;
			tabs = "\t\t";
		}
		else if(s.equalsIgnoreCase("Fantastic"))
		{
			min = 91;
			max = 96;
			tabs = "\t\t";
		}
		else
		{
			min = 96;
			max = 100;
			tabs = "\t\t";
		}
		meanValue = Math.round((min + max)/2 * 10);
		color = determineColor(meanValue);
	}
	 
	public String getDescription() {
		return description;
	}

	public int getMin() {
		return min;
	}

	public int getMax() {
		return max;
	}

	public Color getColor() {
		return color;
	}
	
	public String getHtmlColor()
	{
		return Integer.toHexString(color.getRGB()).substring(2);
	}

	public double getTrueValue() {
		return trueValue;
	}
	
	public int getMeanValue() {
		return meanValue;
	}


	public Attribute(double value){
		trueValue = value;
		meanValue = (int)Math.round(value * 10);
		if(value < 21)
		{
			description = "Disastrous";
			min = 1;
			max = 21;
			tabs = "\t\t";
		}
		else if(value < 31)
		{
			description = "Useless";
			min = 21;
			max = 31;
			tabs = "\t\t";
		}
		else if(value < 41)
		{
			description = "Very Poor";
			min = 31;
			max = 41;
			tabs = "\t\t";
		}
		else if(value < 51)
		{
			description = "Poor";
			min = 41;
			max = 51;
			tabs = "\t\t";
		}
		else if(value < 61)
		{
			description = "Mediocre";
			min = 51;
			max = 61;
			tabs = "\t\t";
		}
		else if(value < 71)
		{
			description = "Means Business";
			min = 61;
			max = 71;
			tabs = "\t";
		}
		else if(value < 81)
		{
			description = "Good";
			min = 71;
			max = 81;
			tabs = "\t\t";
		}
		else if(value < 86)
		{
			description = "Very Good";
			min = 81;
			max = 86;
			tabs = "\t\t";
		}
		else if(value < 91)
		{
			description = "Superb";
			min = 86;
			max = 91;
			tabs = "\t\t";
		}
		else if(value < 96)
		{
			description = "Fantastic";
			min = 91;
			max = 96;
			tabs = "\t\t";
		}
		else
		{
			description = "Star Quality";
			min = 96;
			max = 100;
			tabs = "\t\t";
		}
		color = determineColor(meanValue);
	}
	
	public Color determineColor(int value)
	{
		value = Math.round(value / 1000f * (255 + 255 + 255 + 127));
		int red = 0,
			green = 0,
			blue = 0,
			alpha = 255;
		if( value < 255)
		{
			alpha = value / 3;
			green = 255;
		}
		else if( value < 255 + 255)
		{
			alpha = value / 3;
			blue = value - 255;
			green = (255 + 255) - value;
		}
		else if( value < 255 + 255 + 255)
		{
			alpha = value / 3;
			red = value - (255 + 255);
			blue = 255;
		}
		else if( value < 255 + 255 + 255 + 127)
		{
			blue = ((255 + 255 + 255 + 127) - value) * 2;
			red = 255;
		}
//		System.out.println(red + " " + green + " " + blue + " " + alpha);
		return new Color(red, green, blue, alpha);
	}
	
	@Override
	public String toString() 
	{
		return description + tabs + getBounds();
	}
	
	public String getBounds()
	{
		String tmp = "";
		if(trueValue > 0)
			tmp  = " (" + trueValue + ")";
		else
			tmp = " (" + min + "-" + max + ")";
		return tmp;
	}
	
	public String toStringFile() 
	{
		String tmp = "";
		if(trueValue > 0)
			tmp  = " (" + trueValue + ")";
		else
			tmp = " (" + min + "-" + max + ")";
		return description + tmp;
	}
	
	public double compare(Attribute a)
	{
		double value1 = 0;
		double value2 = 0;
		if(trueValue > 0)
			value1 = trueValue;
		else
			value1 = (min + max) / 2.;
		if(a.trueValue > 0)
			value2 = a.trueValue;
		else
			value2 = (a.min + a.max) / 2.;
		return Math.round((value1 - value2)*100) / 100.;
	}
	
	public static void main(String[] args) {
		Attribute attrib = new Attribute("Mediocre");
		Attribute attrib1 = new Attribute(100);
		
		System.out.println(attrib);
		System.out.println(attrib1);
		System.out.println(attrib.compare(attrib1));
	}
}
